package com.hhkj.admin.bank.model.req;

import com.hhkj.admin.bank.common.cont.BankConst;
import com.hhkj.admin.bank.common.valid.VPublishBank;
import com.hhkj.admin.bank.model.dopo.BankDo;
import com.hhkj.admin.question.domain.Question;
import com.hhkj.admin.question.model.req.QuestionReq;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * BankReq
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/13 10:29
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BankReq extends BankDo
{

    /** 题目分组列表信息 */
    private List<QuestionReq> questionGroupList;

    /** 选题规则信息 */
    @NotNull(groups = {VPublishBank.class}, message = "选题规则不能为空")
    private BankRuleReq ruleReq;

    /** 文件地址路径 */
    @NotNull(groups = {VPublishBank.class}, message = "文件不能为空")
    private String fileUrl;

    public BankReq(String name, Long courseId, Long courseParentId,
                   Long courseBankTypeId, Map<String, Integer> typeAmountConf,
                   Integer caseGroupSize, String remark, List<QuestionReq> questionGroupList, Integer questionAmount)
    {
        this.setName(name);
        this.setTypeCode(BankConst.Type.TOTAL.getCode());
        this.setCourseId(courseId);
        this.setCourseParentId(courseParentId);
        this.setCourseBankTypeId(courseBankTypeId);
        this.setTypeAmountConf(typeAmountConf);
        this.setCaseGroupSize(caseGroupSize);
        this.setRemark(remark);
        this.setQuestionAmount(questionAmount);
        this.questionGroupList = questionGroupList;
    }

    public BankReq(String name, Map<String, Integer> typeAmountConf,
                   Integer caseGroupSize, String remark, List<QuestionReq> questionGroupList, Integer questionAmount)
    {
        this.setName(name);
        this.setTypeCode(BankConst.Type.TOTAL.getCode());
        this.setTypeAmountConf(typeAmountConf);
        this.setCaseGroupSize(caseGroupSize);
        this.setRemark(remark);
        this.setQuestionAmount(questionAmount);
        this.questionGroupList = questionGroupList;
    }

    public void setQuestionGroupList(List<QuestionReq> questionGroupList)
    {
        this.questionGroupList = questionGroupList;
        Map<String, Integer> typeAmountMap = questionGroupList.stream()
                .collect(Collectors.groupingBy(Question::getTypeCode,
                        Collectors.collectingAndThen(Collectors.toList(), List::size)));
        this.setTypeAmountConf(typeAmountMap);
    }
}
